package org.llp.basic.service.impl;

import org.llp.basic.mapper.BaseMapper;
import org.llp.basic.query.BaseQuery;
import org.llp.basic.service.IBaseService;
import org.llp.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    public void add(T t) {
        baseMapper.insert(t);
    }

    @Override
    public void removeById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T getById(Serializable id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageList(BaseQuery query) {
        long total = baseMapper.queryTotal(query);
        if(total<1){
            return new PageList<>();
        }
        List<T> departments = baseMapper.queryDatas(query);
        return new PageList<>(total,departments);
    }

}
